<?php

namespace app\controller\factory;
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");


use app\Auth;
use app\BaseController;
use app\Request;
use lc\helpers\Curl;
use lc\helpers\Helper;
use lc\helpers\Response;
use think\facade\Db;
use Qiniu\Auth as QiNiuAuth;
use Qiniu\Storage\UploadManager;
use think\route\Domain;
class Work extends BaseController
{

    /**
     * 工单数据
     * @return void
     */
    public function workData(Request $request){

        $admin = Auth::$admin;
        $size = $request['size'];
        $title = $request['title'];
        $startDate = $request['startDate'];
        $endDate = $request['endDate'];

        $data = Db::table('xd_store_order_work')
            ->where('store_id',$admin->store_id)
            ->where('work_status',1)
            ->whereLike('work_id|order_id','%'.$title.'%');

        if(!empty($startDate)){
            $data = $data->whereBetweenTime('cteated_date',$startDate,$endDate);
        };
            $data = $data->order('id','desc')
            ->paginate($size);
        return response::success($data);
    }


    /**
     * 工单信息详情
     * @return void
     */
    public function workDataInfo(Request $request){

        $admin = Auth::$admin;

        $id  = $request['id'];
        $data = Db::table('xd_store_order_work')->where('id',$id)->where('store_id',$admin->id)->find();
        $goodsInfo = Db::table('xd_store_order_goodsInfo')->where('oid',$data['order_id'])->select()->each(function ($item){
            $item['fidjson'] = json_decode($item['fidjson']);
            $item['pic'] = json_decode($item['pic']);
            $item['goods_flaw'] = json_decode($item['goods_flaw']);
            return $item;
        });
        $userDataInfo = Db::table('xd_store_order_work_info')->where('work_id',$id)->where('status',0)->select()->each(function ($item){
            $item['img'] = json_decode($item['img']);return $item;
        });
        $factoryDataInfo = Db::table('xd_store_order_work_info')->where('work_id',$id)->where('status',1)->select()->each(function ($item){
            $item['img'] = json_decode($item['img']);return $item;
        });

        if(empty($data)){
            return response::fail('该工单不存在');
        }

        $data['userDataInfo'] = $userDataInfo;
        $data['factoryDataInfo'] = $factoryDataInfo;
        $data['goodsInfo'] = $goodsInfo;

        return response::success($data);
    }


    /**
     * 工厂提交资料
     * @param Request $request
     * @return void
     */
    public function factoryAddInfo(Request $request){

        $admin = Auth::$admin;
        $id = $request['id'];
        $info = $request['info'];
        $img = $request['img'];

        $workData = Db::table('xd_store_order_work')->where('id',$id)->where('store_id',$admin->store_id)->find();

        if(empty($workData)){
            return response::fail('该工单不存在');
        }

        if($workData['is_end'] == 1){
            return response::fail('该工单已结单,无法继续提交');
        }

        $pic = array();
        if(!empty($img)){
            foreach ($img as $k => $v){
                $pic[] = [
                    'img'=>$v['img']
                ];
            }
        }

        $insert = Db::table('xd_store_order_work_info')->insert([
            'work_id'=>$id,
            'explain'=>$info,
            'img'=>json_encode($pic,true),
            'status'=>1,
            'created_date'=>date('Y-m-d H:i:s',time())
        ]);

        if($insert != 0){
            return response::success();
        }
        return response::fail('提交失败');
    }


    /**
     * 工厂申诉
     * @param Request $request
     * @return void
     */
    public function factoryAppeal(Request $request){
        $admin = Auth::$admin;
        $id = $request['id'];
        $info = $request['info'];
        $img = $request['img'];

        $workData = Db::table('xd_store_order_work')->where('id',$id)->where('store_id',$admin->store_id)->find();

        if(empty($workData)){
            return response::fail('该工单不存在');
        }

        if($workData['is_end'] == 1){
            return response::fail('该工单已结单,无法申诉');
        }

        if($workData['appeal_status'] == 1){
            return response::fail('该订单已申诉,无法继续申诉');
        }

        $pic = array();
        if(!empty($img)) {
            foreach ($img as $k => $v) {
                $pic[] = [
                    'img' => $v['img']
                ];
            }
        }

        $update = Db::table('xd_store_order_work')->where('id',$id)->update([
            'appeal_status'=>1,
            'appeal_explain'=>$info,
            'appeal_img'=>json_encode($pic,true),
            'appeal_date'=>date('Y-m-d H:i:s',time())
        ]);

        if($update != 0){
            return response::success();
        }
        return response::fail();
    }

}
